<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

// API版本前缀
Route::prefix('v1')->group(function () {
    
    // 简单测试路由（无需认证）
    Route::get('test/ping', function () {
        return response()->json([
            'success' => true,
            'message' => 'API is working',
            'timestamp' => now()->toISOString()
        ]);
    });
    
    // 测试社区分享路由（无需认证）
    Route::get('test/shares', 'App\Http\Controllers\Api\CommunityShareController@index');
    
    // 测试求助请求路由（无需认证）
    Route::get('test/help-requests', 'App\Http\Controllers\Api\HelpRequestController@index');
    
    // 认证相关路由（无需认证）
    Route::prefix('auth')->group(function () {
        Route::post('register', 'App\Http\Controllers\Api\AuthController@register');
        Route::post('login', 'App\Http\Controllers\Api\AuthController@login');
        Route::post('forgot-password', 'App\Http\Controllers\Api\AuthController@forgotPassword');
        Route::post('reset-password', 'App\Http\Controllers\Api\AuthController@resetPassword');
    });

    // 需要认证的路由
    Route::middleware('auth:api')->group(function () {
        
        // 认证相关（需要登录）
        Route::prefix('auth')->group(function () {
            Route::get('me', 'App\Http\Controllers\Api\AuthController@me');
            Route::post('logout', 'App\Http\Controllers\Api\AuthController@logout');
            Route::post('verify-email', 'App\Http\Controllers\Api\AuthController@verifyEmail');
        });

        // 用户相关
        Route::prefix('user')->group(function () {
            Route::get('profile', 'App\Http\Controllers\Api\UserController@profile');
            Route::put('profile', 'App\Http\Controllers\Api\UserController@updateProfile');
            Route::post('avatar', 'App\Http\Controllers\Api\UserController@uploadAvatar');
            Route::put('password', 'App\Http\Controllers\Api\UserController@changePassword');
        });

        // 用户统计和能力
        Route::get('users/{user}/statistics', 'App\Http\Controllers\Api\UserController@statistics');
        Route::get('users/{user}/abilities', 'App\Http\Controllers\Api\UserAbilityController@index');
        Route::get('users/{user}/abilities/history', 'App\Http\Controllers\Api\UserAbilityController@history');

        // 初始能力测试
        Route::get('assessment/questions', 'App\Http\Controllers\Api\AssessmentController@questions');
        Route::post('users/{user}/assessment', 'App\Http\Controllers\Api\AssessmentController@submitAssessment');

        // 训练相关
        Route::prefix('training')->group(function () {
            // AI角色
            Route::get('characters', 'App\Http\Controllers\Api\TrainingController@characters');
            Route::post('characters/{character}/favorite', 'App\Http\Controllers\Api\TrainingController@favoriteCharacter');
            Route::delete('characters/{character}/favorite', 'App\Http\Controllers\Api\TrainingController@unfavoriteCharacter');
            
            // 训练场景
            Route::get('scenarios', 'App\Http\Controllers\Api\TrainingController@scenarios');
            
            // 训练会话
            Route::post('sessions', 'App\Http\Controllers\Api\TrainingSessionController@create');
            Route::get('sessions/{session}', 'App\Http\Controllers\Api\TrainingSessionController@show');
            Route::post('sessions/{session}/messages', 'App\Http\Controllers\Api\TrainingSessionController@sendMessage');
            Route::post('sessions/{session}/complete', 'App\Http\Controllers\Api\TrainingSessionController@complete');
            Route::delete('sessions/{session}', 'App\Http\Controllers\Api\TrainingSessionController@abandon');
            
            // 训练复盘
            Route::get('sessions/{session}/review', 'App\Http\Controllers\Api\TrainingReviewController@show');
            Route::post('sessions/{session}/replay', 'App\Http\Controllers\Api\TrainingReviewController@replay');
        });

        // 成就系统
        Route::get('achievements', 'App\Http\Controllers\Api\AchievementController@index');
        Route::get('users/{user}/achievements', 'App\Http\Controllers\Api\AchievementController@userAchievements');
        Route::post('users/{user}/achievements/check', 'App\Http\Controllers\Api\AchievementController@checkAchievements');

        // 每日挑战
        Route::prefix('daily-challenges')->group(function () {
            Route::get('today', 'App\Http\Controllers\Api\DailyChallengeController@today');
            Route::post('{challenge}/complete', 'App\Http\Controllers\Api\DailyChallengeController@complete');
            Route::get('users/{user}/history', 'App\Http\Controllers\Api\DailyChallengeController@history');
        });

        // 社区功能
        Route::prefix('community')->group(function () {
            // 社区分享
            Route::get('shares', 'App\Http\Controllers\Api\CommunityShareController@index');
            Route::post('shares', 'App\Http\Controllers\Api\CommunityShareController@store');
            Route::post('shares/{share}/interact', 'App\Http\Controllers\Api\CommunityShareController@interact');
            Route::get('shares/{share}/comments', 'App\Http\Controllers\Api\CommunityShareController@comments');
            
            // 求助站
            Route::get('help-requests', 'App\Http\Controllers\Api\HelpRequestController@index');
            Route::post('help-requests', 'App\Http\Controllers\Api\HelpRequestController@store');
            Route::post('help-requests/{request}/answers', 'App\Http\Controllers\Api\HelpAnswerController@store');
            Route::put('help-answers/{answer}/best', 'App\Http\Controllers\Api\HelpAnswerController@markAsBest');
        });

        // 社交能量站
        Route::get('social-energy', 'App\Http\Controllers\Api\SocialEnergyController@index');

        // 通知系统
        Route::prefix('notifications')->group(function () {
            Route::get('users/{user}', 'App\Http\Controllers\Api\NotificationController@index');
            Route::put('{notification}/read', 'App\Http\Controllers\Api\NotificationController@markAsRead');
        });

        // 段位系统
        Route::get('users/{user}/tier', 'App\Http\Controllers\Api\TierController@show');
        Route::post('users/{user}/tier/upgrade', 'App\Http\Controllers\Api\TierController@upgrade');

        // 内容举报
        Route::post('content/report', 'App\Http\Controllers\Api\ContentModerationController@report');

        // 用户收藏的角色
        Route::get('users/{user}/favorite-characters', 'App\Http\Controllers\Api\UserController@favoriteCharacters');
    });

    // 测试路由（开发环境使用）
    if (app()->environment('local')) {
        Route::prefix('test')->group(function () {
            Route::get('database', 'App\Http\Controllers\Api\TestController@testDatabase');
            Route::post('share', 'App\Http\Controllers\Api\TestController@testCreateShare');
        });
    }
});